package com.ly.cloud.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.commom.constant.StateConstant;
import com.ly.cloud.dto.CodeRuleTemplateDto;
import com.ly.cloud.entity.CodeRuleTemplatePo;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.CodeRuleTemplateMapper;
import com.ly.cloud.service.CodeRuleTemplateService;
import com.ly.cloud.utils.BeanCopyUtils;
import com.ly.cloud.utils.IpUtils;
import com.ly.cloud.vo.CodeRuleTemplateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: pengshiqng
 * @Description:
 * @Date: Create in 2018/12/6
 */
@Service
public class CodeRuleTemplateServiceImpl implements CodeRuleTemplateService {

    @Autowired
    private CodeRuleTemplateMapper codeRuleTemplatePoMapper ;

    public Page<CodeRuleTemplateVo> selectPage(Page<CodeRuleTemplateVo> page, String templateName) {
        try {
            return page.setRecords(codeRuleTemplatePoMapper.selectPage(page, templateName));
        } catch (Exception e) {
            throw new CloudException("查询异常，请重试!", e);
        }
    }

    public int deleteById(Long id) {
        try {
            return codeRuleTemplatePoMapper.deleteById(id);
        } catch (Exception e) {
            throw new DBException("删除编码规则模板失败！", e);
        }
    }


    public int deleteByIds(List<Long> ids) {
        try {
            return codeRuleTemplatePoMapper.deleteBatchIds(ids);
        } catch (Exception e) {
            throw new DBException("批量删除编码规则模板失败！", e);
        }
    }


    public int insert(String loginUserName ,CodeRuleTemplateDto dto) {
        try {
            //转换成po对象
            CodeRuleTemplatePo po = BeanCopyUtils.copyBeanProperties(dto, CodeRuleTemplatePo.class);
            po.setCjr(loginUserName);
            po.setCjsj(new Date());
            po.setIp(IpUtils.getIp());
            //是否启用 新增的时候默认启用
            po.setSfqy(StateConstant.UP);
            return codeRuleTemplatePoMapper.insert(po) ;
        } catch (Exception e) {
            throw new DBException("新增失败！", e);
        }
    }


    public int update(String loginUserName ,CodeRuleTemplateDto dto) {
        try {
            //转换成po对象
            CodeRuleTemplatePo po = BeanCopyUtils.copyBeanProperties(dto, CodeRuleTemplatePo.class);
            po.setXgr(loginUserName);
            po.setXgsj(new Date());
            po.setIp(IpUtils.getIp());
            return codeRuleTemplatePoMapper.updateById(po) ;
        } catch (Exception e) {
            throw new DBException("修改失败！", e);
        }
    }

}
